/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onlylemi.mapview.slice;

import com.onlylemi.mapview.ResourceTable;
import com.onlylemi.mapview.TestData;
import com.onlylemi.mapview.library.MapView;
import com.onlylemi.mapview.library.MapViewListener;
import com.onlylemi.mapview.library.layer.MarkLayer;
import com.onlylemi.mapview.library.layer.RouteLayer;
import com.onlylemi.mapview.library.utils.MapUtils;
import com.onlylemi.mapview.menu.MenuDialog;
import com.onlylemi.mapview.menu.MenuProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Point;
import ohos.media.image.PixelMap;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RouteLayerTestSlice extends AbilitySlice implements Component.ClickedListener {

    private MapView mapView;

    private MarkLayer markLayer;
    private RouteLayer routeLayer;

    private List<Point> nodes;
    private List<Point> nodesContract;
    private List<Point> marks;
    private List<String> marksName;

    private List<String> itemList;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_route_layer_test);

        Text tvTitle = (Text) findComponentById(ResourceTable.Id_tv_title);
        tvTitle.setText("RouteLayerTest");
        Image ivMenu = (Image) findComponentById(ResourceTable.Id_iv_more);
        ivMenu.setVisibility(Component.VISIBLE);
        ivMenu.setClickedListener(this);

        itemList = new ArrayList<>();
        itemList.add("Genetic For TSP");

        nodes = TestData.getNodesList();
        nodesContract = TestData.getNodesContactList();
        marks = TestData.getMarks();
        marksName = TestData.getMarksName();
        MapUtils.init(nodes.size(), nodesContract.size());

        mapView = (MapView) findComponentById(ResourceTable.Id_mapview);
        PixelMap bitmap = MapUtils.getPixelMap(this, ResourceTable.Media_map);
        mapView.loadMap(bitmap);
        mapView.setMapViewListener(new MapViewListener() {
            @Override
            public void onMapLoadSuccess() {
                routeLayer = new RouteLayer(mapView);
                mapView.addLayer(routeLayer);

                markLayer = new MarkLayer(mapView, marks, marksName);
                mapView.addLayer(markLayer);
                markLayer.setMarkIsClickListener(new MarkLayer.MarkIsClickListener() {
                    @Override
                    public void markIsClick(int num) {
                        Point target = new Point(marks.get(num).getPointX(), marks.get(num).getPointY());
                        List<Integer> routeList = MapUtils.getShortestDistanceBetweenTwoPoints(marks.get(39),
                                target, nodes, nodesContract);
                        routeLayer.setNodeList(nodes);
                        routeLayer.setRouteList(routeList);
                        mapView.refresh();
                    }
                });
                mapView.refresh();
            }

            @Override
            public void onMapLoadFail() {
            }
        });
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_iv_more:
                MenuDialog.with(this)
                        .load(itemList)
                        .setListener(new MenuDialog.OnItemClickListener() {
                            @Override
                            public void onItemClick(MenuProvider adapter, int position) {
                                switch (position) {
                                    case 0:
                                        List<Point> list = new ArrayList<>();
                                        list.add(marks.get(39));
                                        list.add(marks.get(new SecureRandom().nextInt(10)));
                                        list.add(marks.get(new SecureRandom().nextInt(10) + 10));
                                        list.add(marks.get(new SecureRandom().nextInt(10) + 20));
                                        list.add(marks.get(new SecureRandom().nextInt(10) + 9));
                                        List<Integer> routeList = MapUtils.getBestPathBetweenPoints(list, nodes,
                                                nodesContract);
                                        routeLayer.setNodeList(nodes);
                                        routeLayer.setRouteList(routeList);
                                        mapView.refresh();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }).showDialog();
                break;
            default:
                break;
        }
    }
}
